<?php

class Tool
{
    public static $php_input = false;
    public static $php_header = false;

    // region 获取 Input 参数
    public static function gi($key, $default = false)
    {
        if (self::$php_input == false) {
            $content = file_get_contents("php://input");
            if (!defined('Decode')) {
                switch (self::ini('EncodeType', 'none')) {
                    case 'base64':
                        $content = base64_decode($content);
                        break;
                    case 'rc4':
                        $content = Rc4::decode($content, self::ini('Rc4Key'));
                        break;
                }
            }
            $check = Check::DecodeInput($content);
            if (!$check['check']) self::exit($check);
            $content = json_decode($content, true);
            self::$php_input = $content;
        } else $content = self::$php_input;
        return isset($content[$key]) ? $content[$key] : $default;
    }
    // endregion
    // region 获取 Post 参数
    public static function gp($key, $default = false)
    {
        return isset($_POST[$key]) ? $_POST[$key] : $default;
    }
    // endregion
    // region 获取 Cookie 参数
    public static function gc($key, $default = false)
    {
        return isset($_COOKIE[$key]) && $_COOKIE[$key] !== false ? $_COOKIE[$key] : $default;
    }
    // endregion
    // region 获取 Header 参数
    public static function gh($key, $default = false)
    {
        if (self::$php_header == false) {
            $content = [];
            foreach ($_SERVER as $name => $value) {
                if (substr($name, 0, 5) == 'HTTP_') {
                    $content[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
                }
            }
            self::$php_input = $content;
        } else $content = self::$php_header;
        return isset($content[$key]) ? $content[$key] : $default;
    }
    // endregion
    // region 获取 Get 参数
    public static function gg($key, $default = false)
    {
        return isset($_GET[$key]) ? $_GET[$key] : $default;
    }
    // endregion
    // region 获取 URL / 参数
    public static function gu($key, $default = false)
    {
        $router_link = mb_strlen($_SERVER['QUERY_STRING'])
            ? explode('?' . $_SERVER['QUERY_STRING'], $_SERVER['REQUEST_URI'])[0]
            : $_SERVER['REQUEST_URI'];
        $router_array = explode('/', $router_link);
        return isset($router_array[$key]) ? $router_array[$key] : $default;
    }
    // endregion
    // region 去除空格
    public static function ge($str)
    {
        return preg_replace("/\s+/", ' ', $str);
    }
    // endregion
    // region 获取 IP
    public static function ip()
    {
        //strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
        if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $ip = getenv('REMOTE_ADDR');
        } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $res = preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches [0] : '';
        return $res;
    }
    // endregion
    // region 获取完整路径
    public static function fp($path)
    {
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        return $http_type . $_SERVER['HTTP_HOST'] . $path;
    }
    // endregion
    // region 获取后台 Path
    public static function ap($path)
    {
        return '/' . self::ini('AdminPath', 'Admin') . $path;
    }
    // endregion
    // region 10位时间戳 格式化
    public static function date($time = false, $format = "Y-m-d H:i:s")
    {
        if (!$time) $time = time();
        return date($format, $time);
    }
    // endregion
    // region 毫秒时间戳
    public static function time()
    {
        return floor(microtime(true) * 1000);
    }
    // endregion
    // region exit
    public static function exit($data = [])
    {
        $res = json_encode($data, JSON_UNESCAPED_UNICODE);
        if (!defined('Encode')) {
            $encode_type = self::ini('EncodeType', 'none');
            switch ($encode_type) {
                case 'base64':
                    $res = base64_encode($res);
                    break;
                case 'rc4':
                    $res = Rc4::encode($res, self::ini('Rc4Key', 'NQi7b2Md'));
                    break;
            }
        }
        exit($res);
    }
    // endregion
    // region echo
    public static function echo($message = '', $code = 200, $data = [])
    {
        $return = [];
        $return['code'] = $code;
        if ($message) $return['message'] = $message;
        if (count($data)) $return['data'] = $data;
        self::exit($return);
    }
    // endregion
    // region 读取 Config ini
    public static function ini($key, $default = false, $path = CONFIG_INI)
    {
        if (!$key) return $default;
        $config_file_path = $path;
        $config_info = parse_ini_file($config_file_path);
        return (isset($config_info[$key])) ? $config_info[$key] : $default;
    }
    // endregion
}